<?php

namespace App\Http\Controllers\shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
// 管理员用户模型类
use App\Model\AdminUserModel;
use App\Model\AdminLogsModel;

class LoginController extends Controller
{   
    //登陆页
    public function show(Request $request)
    {
        return view('shop.login');
    }

    //提交登录数据
    public function action(Request $request)
    {
        $user = $request->post('user');//账号
        $password = $request->post('password');//密码

        $auModel = new AdminUserModel;//管理员表

        $res = $auModel->where('user',$user)->where('type',2)->first();
        if(!$res){
            return returnJson(0,'没有该用户');
        }
        //验证状态
        if($res['state']['value'] != 1){
            return returnJson(0,'该用户已被冻结，请联系管理员进行激活');
        }
        //验证密码
        if($res['password'] != passEncryp($password)){            
            return returnJson(0,'密码错误');
        }
        //存session
        session()->put('shop_id', $res['id']);
        AdminLogsModel::addLogs('登录【货源后台】');
        return returnJson(1,'登陆成功');
    }

    //退出
    public function loginOut(Request $request)
    {
        AdminLogsModel::addLogs('退出【货源后台】');
        session()->flush();
        // return redirect(url('admin/login'));
        return redirect('shop/login');
    }

  /*


  // 提交登录数据
  public function action (Request $request, AdminUserModel $admin_user){
    $user = $request->input('user');
    $findUser = $admin_user->where('user', $user)->first();
    if($findUser){
      if($findUser['password'] == md5($request->input('password'))){
        if(!$findUser['state']){
          return ['code' => 0, 'msg' => '该用户已被冻结，请联系管理员进行激活'];
        }
        $request->session()->put('id',$findUser['id']);
        $request->session()->put('user',$findUser['user']);
        $request->session()->put('state',$findUser['state']);
        $request->session()->put('role',$findUser['role']);
        $request->session()->put('offset',$request->input('offset'));
        AdminLogsModel::addLog('登录');
        return response()->json(['code' => 1, 'msg' => '登录成功']);
      }else{
        return ['code' => 0, 'msg' => '密码错误'];
      }
    }else{
      return ['code' => 0, 'msg' => '没有该用户'];
    }
  }
  
  // 获取路由前缀
  public function getAdminPrefix($request) {
    $adminPrefix = explode('/',$request->route()->uri)[0];
    return $adminPrefix;
  }

  // 退出
  public function loginOut(Request $request) {
    AdminLogsModel::addLog('退出');
    $request->session()->flush();
    return redirect($this->getAdminPrefix($request).'/login');
  }
  */
}
